package vs_a1;

import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Message:
 * contains all necessary information about a message received by the Server.
 * These information are: ID of message and client, the message itself and the time where it was sent.
 * @author Jan Quenzel
 * @author Rico Flaegel
 */
public class Message {
	/**
	 * MessageID generator.
	 */
	private static AtomicLong al = new AtomicLong(0);
	private long messageID;
	private String clientID;
	private String message;
	private long time;
	/**
	 * Constructor for a message
	 * @param clientID the ID of the client, used to identify the client.
	 * @param message the message itself, send by the client
	 * @param time the time when it received by the Server
	 */
	public Message(String clientID, String message, long time) {
		this.messageID = al.getAndIncrement();
		this.clientID = clientID;
		this.message = message;
		this.time = time;
	}
	/**
	 * Get the MessageID.
	 * @return the MessageID.
	 */
	public long getMessageID() {
		return messageID;
	}
	/**
	 * Get the ClientID.
	 * @return the clientID
	 */
	public String getClientID() {
		return clientID;
	}
	/**
	 * Get the message itself only.
	 * @return the message itself, send by client.
	 */
	public String getMessage() {
		return message;
	}
	/**
	 * Get the Timestamp.
	 * @return a Date.
	 */
	public Date getTimestamp() {
		return new Date(time);
	}
	/**
	 * Get a String containing all necessary information about the Message.
	 * @return all information from the message as a String
	 */
	public String deliverMessage(){
		return messageID + " " + clientID + " " + message +  " " + getTimestamp().toString();
	}
	
	@Override
	public String toString(){
		return deliverMessage();
	}

	@Override
	public boolean equals(Object o){
		if( o instanceof Message){
			Message m = (Message) o;
			return this.clientID.equals(m.clientID) && this.message.equals(m.message);
		}
		return false;
	}
}
